{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json, os, re\n",
    "from IPython.core.display import display, HTML"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "def convert(code):\n",
    "    converted = []\n",
    "    for line in code:\n",
    "        if line.startswith('#--snip'):\n",
    "            break\n",
    "        m = re.match(r'^(\\s*)(.+)#([^ ].*)$', line)\n",
    "        converted.append((m.group(1) + m.group(3) + '\\n') if m else line)\n",
    "    return converted"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./0_intro.ipynb\">./0_intro.ipynb</a> (15293 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/0_intro.ipynb\">../exercises/0_intro.ipynb</a> (3897 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./1_qd_data.ipynb\">./1_qd_data.ipynb</a> (262138 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/1_qd_data.ipynb\">../exercises/1_qd_data.ipynb</a> (24269 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./2_tf_basics.ipynb\">./2_tf_basics.ipynb</a> (37088 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/2_tf_basics.ipynb\">../exercises/2_tf_basics.ipynb</a> (23102 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./3_tf_ml.ipynb\">./3_tf_ml.ipynb</a> (58532 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/3_tf_ml.ipynb\">../exercises/3_tf_ml.ipynb</a> (10777 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./4_qd_estimator.ipynb\">./4_qd_estimator.ipynb</a> (116527 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/4_qd_estimator.ipynb\">../exercises/4_qd_estimator.ipynb</a> (42753 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<li><a target=\"_blank\" href=\"./5_qd_cloud.ipynb\">./5_qd_cloud.ipynb</a> (10425 bytes) &#8680; <a target=\"_blank\" href=\"../exercises/5_qd_cloud.ipynb\">../exercises/5_qd_cloud.ipynb</a> (8012 bytes)</li>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "SOURCE = '.'\n",
    "DESTINATION = '../exercises'\n",
    "NOTE_FMT = '### NOTE: This file is auto-generated from \"%s\".\\n\\n'\n",
    "LINK_FMT = ('<li><a target=\"_blank\" href=\"%s\">%s</a> (%d bytes) &#8680; '\n",
    "            '<a target=\"_blank\" href=\"%s\">%s</a> (%d bytes)</li>')\n",
    "if not os.path.exists(DESTINATION):\n",
    "    os.mkdir(DESTINATION)\n",
    "display(HTML('<pre>'))\n",
    "for name in os.listdir(SOURCE):\n",
    "    if not name.endswith('.ipynb') or name.startswith('_'):\n",
    "        continue\n",
    "    path = os.path.join(SOURCE, name)\n",
    "    nb = json.load(file(path))\n",
    "    for cell in nb['cells']:\n",
    "        if cell['cell_type'] == 'code':\n",
    "            cell['source'] = convert(cell['source'])\n",
    "        if 'outputs' in cell: cell['outputs'] = []\n",
    "        if 'execution_count' in cell: cell['execution_count'] = None\n",
    "#     nb['cells'][0]['source'].insert(0, NOTE_FMT % os.path.abspath(path))\n",
    "    path2 = os.path.join(DESTINATION, name)\n",
    "    json.dump(nb, file(path2, 'w'))\n",
    "    display(HTML(LINK_FMT % (path, path, os.path.getsize(path),\n",
    "                             path2, path2, os.path.getsize(path2))))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
